﻿using MapGeneration;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoundEvent.Main;
using System.Linq;
using UnityEngine;
using Utils;


namespace RoundEvent.Event
{
    public class BlackEvent
    {
        string[] commands = new string[] { "event", "eve" };

        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            string command = ev.Command.ToLower();

            if (!commands.Contains(command)) return;

            if (ev.Arguments[0] != "black") return;


            if (ev.Arguments.Length == 1)
            {
                this.DoNoticeBlack();
                return;
            }


            if (ev.Arguments.Length == 2 && ev.Arguments[1] == "done")
            {
                this.DoNoticeBlackDone();
                return;
            }
        }

        [PluginEvent(ServerEventType.RoundStart)]
        public void OnRoundStart(RoundStartEvent ev)
        {
            bool isHappened =  UnityEngine.Random.Range(0, 10) <= CodeLoad.Singleton.Config.BlackEvent.HappenedPercent;

            if (isHappened)
            {
                TimeCoroutine.Delay(35f, () => {  this.DoNoticeBlack();  });
            }
        }



        Coroutine m_BlackCOR;

        private void DoNoticeBlack()
        {
            Cassie.Message("WARNING ALL THE FORCE BLACK ZONE AS FAQ LIGHT");
            Cassie.Message("<color=red>[轻收容区域警告]</color> 监测电压异常值变化，即将启用备用微光应急照明系统", false, false, true);

            Map.Rooms.ToList().FindAll(e => e.Zone == FacilityZone.LightContainment).ForEach(room =>
            {
                if (room.ApiRoom.TryGetComponent(out RoomLightController component, false, true))
                {
                    component.NetworkOverrideColor = new UnityEngine.Color(0.1f, 0.1f, 0.1f, 0.1f);
                }
            });
        }
        private void DoNoticeBlackDone()
        {
            Cassie.Message("ATTENTION ALL THE FORCE BLACK ZONE AS FAQ LIGHT");
            Cassie.Message("<color=green>[通知]</color> 轻收容区域供电已恢复正常", false, false, true);

            Map.Rooms.ToList().FindAll(e => e.Zone == FacilityZone.LightContainment).ForEach(room =>
            {
                if (room.ApiRoom.TryGetComponent(out RoomLightController component, false, true))
                {
                    component.NetworkOverrideColor = new UnityEngine.Color(1f, 1f, 1f, 1f);
                }
            });
        }
    }
}